package com.kingtsoft.study.service.impl;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kingtsoft.study.common.ContextHolder;
import com.kingtsoft.study.constant.DescribableEnum;
import com.kingtsoft.study.constant.ParamConstants;
import com.kingtsoft.study.exception.BusinessException;
import com.kingtsoft.study.model.StudyStudent;
import com.kingtsoft.study.model.StudyTeacher;
import com.kingtsoft.study.model.StudyUser;
import com.kingtsoft.study.persist.StudyStudentMapper;
import com.kingtsoft.study.persist.StudyTeacherMapper;
import com.kingtsoft.study.persist.StudyUserMapper;
import com.kingtsoft.study.service.StudyUserService;
import com.kingtsoft.study.util.MD5;

/**
 * 
 * @ClassName: StudyUserService
 * @Description: 用户相关service实现类
 * @author: shenlx
 * @date: 2017年3月29日 上午8:51:12
 */
@Service
public class StudyUserServiceImpl implements StudyUserService {

	@Autowired
	private StudyUserMapper studyUserMapper;
	@Autowired
	private StudyStudentMapper studyStudentMapper;
	@Autowired
	private StudyTeacherMapper studyTeacherMapper;

	/**
	 * 
	 * @Title: doLogin
	 * @Description: 用户登录校验
	 * @author: shenlx
	 * @param studyUser
	 * @return: void
	 */
	@Override
	public void doLogin(StudyUser studyUser) {

		if (null == studyUser) {
			throw new BusinessException(DescribableEnum.UNKNOWNUSER);// 用户名不存在
		} else {
			if (StringUtils.isBlank(studyUser.getUsername())) {
				throw new BusinessException(DescribableEnum.UNKNOWNUSER);// 用户名不存在
			} else {
				StudyUser u = studyUserMapper.selectByUserName(studyUser.getUsername());
				if (null == u) {
					throw new BusinessException(DescribableEnum.UNKNOWNUSER);// 用户名不存在
				} else {
					if (StringUtils.equals(MD5.getMD5(studyUser.getPassword()), u.getPassword())) {

						if (StringUtils.equals(u.getUserType(), ParamConstants.USER_TYPE_0)) {
							System.out.println("这是一个学生===================");

							StudyStudent student = studyStudentMapper.selectByUserId(u.getId());

							ContextHolder.getSession().setAttribute(ParamConstants.LOCAL_CLINET_STUDENT, student);

						} else if (StringUtils.equals(u.getUserType(), ParamConstants.USER_TYPE_1)) {
							System.out.println("这是一个老师===================");

							StudyTeacher teacher = studyTeacherMapper.selectByUserId(u.getId());

							ContextHolder.getSession().setAttribute(ParamConstants.LOCAL_CLINET_TEACHER, teacher);

						} else {
							System.out.println("未指定用户类型===================");
						}

						// 存放用户对象到session中
						ContextHolder.getSession().setAttribute(ParamConstants.LOCAL_CLINET_USER, u);

					} else {
						throw new BusinessException(DescribableEnum.ERROR_PWD);// 密码错误
					}
				}
			}
		}

	}

	/**
	 * 
	 * @Title: remoteUsername
	 * @Description: 校验用户名唯一性
	 * @author: shenlx
	 * @param username
	 * @return
	 * @return: boolean
	 */
	@Override
	public boolean remoteUsername(String username) {
		StudyUser u = studyUserMapper.selectByUserName(username);
		if (null != u) {// 用户已经存在
			return false;
		} else {
			return true;
		}

	}

	/**
	 * 
	 * @Title: addRegister
	 * @Description: 用户注册
	 * @author: shenlx
	 * @param studyUser
	 * @param studentName
	 *            这里为了方便，只做了学生注册，并只保存学生姓名
	 * @return
	 */
	@Override
	public void addRegister(StudyUser studyUser, String studentName) {
		studyUser.setUserType(ParamConstants.USER_TYPE_0);
		studyUser.setPassword(MD5.getMD5(studyUser.getPassword()));
		studyUserMapper.insert(studyUser);
		
		StudyStudent student = new StudyStudent();
		student.setUserId(studyUser.getId());
		student.setStudentName(studentName);
		studyStudentMapper.insert(student);
	}

	/**
	 * 获取登录人员名字
	 */
	public String getLoginName() {
		try {
			HttpSession session = ContextHolder.getSession();
			StudyUser u = ContextHolder.getSessionUser();
			if (u == null)
				return "";
			if (u.getUserType().equals(ParamConstants.USER_TYPE_0)) {
				return ContextHolder.getSessionStudent().getStudentName();
			} else if (u.getUserType().equals(ParamConstants.USER_TYPE_1)) {
				return ContextHolder.getSessionTeacher().getTeacherName();
			}
			return u.getUsername();
		} catch (Exception e) {
			e.printStackTrace();
			return "Error";
		}
	}

	/**
	 * 登出
	 */
	@Override
	public void doLogout() {
		HttpSession session = ContextHolder.getSession();
		if (session != null) {
			session.removeAttribute(ParamConstants.LOCAL_CLINET_USER);
			session.removeAttribute(ParamConstants.LOCAL_CLINET_STUDENT);
			session.removeAttribute(ParamConstants.LOCAL_CLINET_TEACHER);
			session.invalidate();
		}
	}

}
